const SFCScriptBlock = require('@vue/compiler-sfc')
const {getEmitsNode} = require("./relNode");

/**
 * 解析 emits
 * @param {SFCScriptBlock} scriptBlock vue解析后的Script块
 * @return {{
 *     name: string,
 *     desc: string,
 *     comments: string[]
 * }|null}
 */
function relEmits(scriptBlock) {
    const emitsNode = getEmitsNode(scriptBlock)
    if (!emitsNode) return null
    const emitsList = []
    const arguments = emitsNode.arguments
    if (arguments && arguments.length > 0) {
        const elements = arguments[0].elements
        if (elements && elements.length > 0) {
            for (let element of elements) {
                const emitInfo = {
                    name: element.value,
                    desc: '',
                    comments: []
                }
                // todo 解析调用，获取参数等
                const leadingComments = element.leadingComments
                if (leadingComments && leadingComments.length > 0) {
                    leadingComments.forEach((comment, index) => {
                        if (index === 0) {
                            emitInfo.desc = comment.value
                        } else {
                            emitInfo.comments.push(comment.value)
                        }
                    })
                }
                emitsList.push(emitInfo)
            }
        }
    }
    return emitsList
}

exports.relEmits = relEmits;
